# -*- coding: utf-8 -*-
import time
import LogUtil as L
@L.log()
def get_html_tr(case_name, case_result, case_img, case_per, device_log, filter_log,cost_time):
    """

        :param case_name:编号
        :param case_result: 文本
        :param case_img:图片地址
        :return: html tr部分
        """
    tr = """
        <tr bgcolor="MintCream">
                %(case_id)s
                %(pass)s
                %(img)s
                %(per)s
                %(device_log)s
                %(cost_time)s
        </tr>
        """

    case_id = '<td>{}</td>'.format(case_name)
    passing_state = '<td>{}</td>'.format(case_result)
    img = '<td><img src="{}" align="absmiddle" width="350" height="250"/></td>'.format(case_img)
    per = '<td><img src="{}" align="absmiddle" width="350" height="250"/></td>'.format(case_per)
    device_log = '<td><a href="{}">device_log</a></td>'.format(device_log)
    # filter_log = '<td><a href="{}">device_filter_log</a></td>'.format(filter_log)
    cost_time = '<td>{}</td>'.format(cost_time)

    result = {'case_id': case_id, 'pass': passing_state, 'img': img, 'per': per, 'device_log': device_log,
              'filter_log': filter_log,"cost_time":cost_time}
    return tr % result

@L.log()
def make_html(log, device, app_info, test_status, count_result,result_path,show_statistic_image=True):
    """
    :param log: 测试报告报表
    :param device: device信息
    :param app_info: 测试应用信息
    :param test_status: 测试用例信息
    :param count_result 包含所有测试用列结果
    :param result_path: 输出文件夹
    :return:
    """
    all_case=len(count_result)
    passed=count_result.count(True)
    failed = count_result.count(False)
    template = '''
    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
        <title>Test Report</title>
    </head>
    <body>
    <span style="color:green;"><h1>Test Report</h1></span>
    <p>End Time:{Time}</p>
    <p>{device}</p>
    <p>{app_info}</p>
    <p><span style="color:blue;">All_Case:{All_Case},<span style="color:green;">passed:{passed},<span style="color:red;">failed:{failed}</p>
    <table>
    </table>
    <table border="1" cellpadding="10">
        <tbody>
        <tr bgcolor="MintCream">
            <th>用列名称</th>
            <th>测试结果</th>
            <th>测试截图</th>
            <th>CPU/MEM</th>
            <th>测试日志</th>
            <th>比对时间(ms)</th>
        </tr>
            %(tr)s
        </tbody>
    </table>
    %(st)s
    </body>
    </html>
    '''.format(Time= time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), device=device, app_info=app_info, All_Case=all_case, passed=passed, failed=failed)
    if show_statistic_image:
        statistic_img = '''<img src="{}" align="absmiddle" width="800" height="500"/><img src="{}" align="absmiddle" width="800" height="500"/>'''.format(result_path+"/statistic.png",result_path+"/mem.png")
        data={"st": statistic_img,'tr': log}
        pass
    else:
        data = {"st": "",'tr': log}
        pass

    # data = {'tr': log}
    save_html_file = '%s/test.html' % result_path
    with open(save_html_file, 'w') as f:
        f.write(template % data)
        f.close()
    return save_html_file